package com.mgs.product.tagger.handlers
{
	import com.mgs.fwk.context.ApplicationContext;
	import com.mgs.fwk.handlers.ActionHandler;
	import com.mgs.product.tagger.controller.ContextController;
	import com.mgs.product.tagger.actions.ApplicationAction;
	import com.mgs.product.tagger.actions.SwitchRepositoryAction;
	import com.mgs.product.tagger.dao.ClassificationDao;
	import com.mgs.product.tagger.dao.ClassificationDaoImpl;
	import com.mgs.product.tagger.ui.dto.NavigationContext;
	
	public class SwitchRepositoryHandler implements ActionHandler
	{
		internal var classificationDao:ClassificationDao = new ClassificationDaoImpl();
		
		public function execute(context:ApplicationContext, action:ApplicationAction):void
		{
			// save previous context (if any)
			if((context as NavigationContext).repository != null)
			{
				classificationDao.save((context as NavigationContext).repository, (context as NavigationContext).classification);
			}
			// load context.
			ContextController.createContext((action as SwitchRepositoryAction).newRepository, (context as NavigationContext));
			if((action as SwitchRepositoryAction).useAsDefaultRepository)
			{
				(context as NavigationContext).preferences.defaultRepository = (action as SwitchRepositoryAction).newRepository;
			}
			else
			{
				(context as NavigationContext).preferences.defaultRepository = null;
			}
		}
	}
}